package com.fmdj.nebula.controller;

import com.fmdj.common.exception.GlobalException;
import com.fmdj.common.util.CommonResult;
import com.fmdj.nebula.controller.form.InsertOrderMonitoringForm;
import com.fmdj.nebula.service.MonitoringService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

@RestController
@RequestMapping("/monitoring")
@Tag(name = "MonitoringController", description = "监控服务的web接口")
@Slf4j
public class MonitoringController {

    @Autowired
    private MonitoringService monitoringService;

    @PostMapping(value = "/uploadRecordFile")
    @Operation(summary = "上传监控录音文件")
    public CommonResult uploadRecordFile(
                                            @RequestPart("file") MultipartFile file,
                                            @RequestPart("name") String name,
                                            @RequestPart("text ") String text) { //@RequestPart注解的作用:  上传文件时，将文件封装成MultipartFile对象，将文件名和文件描述封装成Map对象，将Map对象封装成JSON字符串，将JSON字符串封装成对象，将对象封装成Map对象，将Map对象

        if (file.isEmpty()) {
            throw new GlobalException("上传的文件不能为空");
        }
        monitoringService.monitoring(file, name, text);

        return CommonResult.ok();
    }

    @PostMapping(value = "/insertOrderMonitoring")
    @Operation(summary = "插入订单监控摘要记录")
    public CommonResult insertOrderMonitoring(@RequestBody @Valid InsertOrderMonitoringForm form) {

        int rows = monitoringService.insertOrderMonitoring(form.getOrderId());

        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }
}
